﻿using ChinaTokenDev.Logger.Enum;
using System;
using System.Diagnostics;
using System.IO;

namespace ChinaTokenDev.Logger
{
    /// <summary>
    /// 日志适配器
    /// </summary>
    public static class LogHelper
    {
        /// <summary>
        /// 日志对象
        /// </summary>
        private readonly static ILogAdapter _runtimeLogger = new TxtLogHelper();
        /// <summary>
        /// 默认日志文件名
        /// </summary>
        private readonly static string _defaultLogName = "SysMonitor";
        /// <summary>
        /// 日志默认后缀格式
        /// </summary>
        private static EnumLogFormatType _paramFormatType = EnumLogFormatType.Log;
        /// <summary>
        /// 日志输出级别
        /// </summary>
        private static EnumLogLevel _paramLogLevel = EnumLogLevel.All;

        /// <summary>
        /// 配置日志输出格式
        /// </summary>
        /// <param name="logLevel">日志等级</param>
        /// <param name="formatType">日志类型</param>
        public static void Init(EnumLogLevel logLevel = EnumLogLevel.All, EnumLogFormatType formatType = EnumLogFormatType.Log)
        {
            _paramFormatType = formatType;
            _paramLogLevel = logLevel;
        }

        /// <summary>
        /// 按天清理过期日志
        /// </summary>
        /// <param name="day">天</param>
        public static void CleanLogs(int day)
        {
            ClearLoger clearLoger = new ClearLoger();
            clearLoger.Clean(day);
        }

        #region Info
        /// <summary>
        /// Info类型日志 默认输出文件名为 SysMonitor
        /// </summary>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteInfoLog(string msgformat, params object[] args)
        {
            WriteInfoLogExt(_defaultLogName, msgformat, args);
        }

        /// <summary>
        /// Info类型日志 默认输出文件名为 SysMonitor
        /// </summary>
        /// <param name="msg">日志消息体</param>
        public static void WriteInfoLog(string msg)
        {
            WriteInfoLogEx(_defaultLogName, msg);
        }

        /// <summary>
        /// Info类型日志
        /// </summary>
        /// <param name="logName">日志文件名</param>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteInfoLogExt(string logName, string msgformat, params object[] args)
        {
            WriteInfoLogEx(logName, string.Format(msgformat, args));
        }

        /// <summary>
        /// Info类型日志
        /// </summary>
        /// <param name="logName">日志文件名</param>
        /// <param name="msg">日志消息体</param>
        public static void WriteInfoLogEx(string logName, string msg)
        {
            PushLogger(logName, EnumLogLevel.Info, msg);
        }
        #endregion Info

        #region Debug
        /// <summary>
        /// Debug类型日志 默认输出名称为SystemMonitor
        /// </summary>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteDebugLog(string msgformat, params object[] args)
        {
            WriteDebugLogExt(_defaultLogName, msgformat, args);
        }

        /// <summary>
        /// Debug类型日志
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteDebugLog(string msg)
        {
            WriteDebugLogEx(_defaultLogName, msg);
        }

        /// <summary>
        /// Debug类型日志
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteDebugLogExt(string logName, string msgformat, params object[] args)
        {
            WriteDebugLogEx(logName, string.Format(msgformat, args));
        }

        /// <summary>
        /// Debug类型日志
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="msg"></param>
        public static void WriteDebugLogEx(string logName, string msg)
        {
            PushLogger(logName, EnumLogLevel.Debug, msg);
        }
        #endregion Debug

        #region Warning
        /// <summary>
        /// Warning类型日志
        /// </summary>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteWarningLog(string msgformat, params object[] args)
        {
            WriteWarningLogExt(_defaultLogName, msgformat, args);
        }

        /// <summary>
        /// Warning类型日志
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteWarningLog(string msg)
        {
            WriteWarningLogEx(_defaultLogName, msg);
        }

        /// <summary>
        /// Warning类型日志
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteWarningLogExt(string logName, string msgformat, params object[] args)
        {
            WriteWarningLogEx(logName, string.Format(msgformat, args));
        }
        
        /// <summary>
        /// Warning类型日志
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="msg"></param>
        public static void WriteWarningLogEx(string logName, string msg)
        {
            PushLogger(logName, EnumLogLevel.Warning, msg);
        }
        #endregion Warning

        #region Error
        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg"></param>
        /// <param name="logName"></param>
        public static void WriteErrorFormat(int code, string msg, string logName = "")
        {
            string errMsg = string.Format("{0}:{1}", code, msg);
            PushLogger(logName, EnumLogLevel.Error, errMsg);
        }
        /// <summary>
        /// Error类型日志
        /// </summary>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteErrorLog(string msgformat, params object[] args)
        {
            WriteErrorLogExt(_defaultLogName, msgformat, args);
        }

        /// <summary>
        /// Error类型日志
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteErrorLog(string msg)
        {
            WriteErrorLogEx(_defaultLogName, msg);
        }

        /// <summary>
        /// Error类型日志
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="msgformat"></param>
        /// <param name="args"></param>
        public static void WriteErrorLogExt(string logName, string msgformat, params object[] args)
        {
            WriteErrorLogEx(logName, string.Format(msgformat, args));
        }
        
        /// <summary>
        /// Error类型日志
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="msg"></param>
        public static void WriteErrorLogEx(string logName, string msg)
        {
            PushLogger(logName, EnumLogLevel.Error, msg);
        }
        #endregion Error

        /// <summary>
        /// 消息体输出
        /// </summary>
        /// <param name="logName">日志文件名</param>
        /// <param name="logLevel">日志级别</param>
        /// <param name="msg">日志消息体</param>
        private static void PushLogger(string logName, EnumLogLevel logLevel, string msg)
        {
            //string dir = Path.Combine(System.Threading.Thread.GetDomain().BaseDirectory, "Logs", DateTime.Now.ToString("yyyyMM"));
            string dir = Path.Combine(System.Threading.Thread.GetDomain().BaseDirectory, _runtimeLogger.LogRootDir, DateTime.Now.ToString("yyyyMM"), DateTime.Now.ToString("dd"));
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            string fileName = string.Empty;
            if (string.IsNullOrWhiteSpace(logName))
            {
                logName = _defaultLogName;
                //fileName = Path.Combine(dir, _defaultLogName, ".log");
            }
            //什么情况下后缀名用log，什么时候后缀用txt？
            switch (_paramFormatType)
            {
                case EnumLogFormatType.Txt:
                    fileName = Path.Combine(dir, string.Concat(logName, "log.txt"));
                    break;
                default:
                    fileName = Path.Combine(dir, string.Concat(logName, ".log"));
                    break;
            }

            //根据日志等级输出不同的日志
            switch (logLevel)
            {
                case EnumLogLevel.Error:
                    if (_paramLogLevel >= EnumLogLevel.Error) _runtimeLogger.WriteErrLog(fileName, msg);
                    break;
                case EnumLogLevel.Warning:
                    if (_paramLogLevel >= EnumLogLevel.Warning) _runtimeLogger.WriteWarnLog(fileName, msg);
                    break;
                case EnumLogLevel.Debug:
                    if (_paramLogLevel >= EnumLogLevel.Debug) _runtimeLogger.WriteDebugLog(fileName, msg);
                    break;
                default:
                    if (_paramLogLevel >= EnumLogLevel.Info) _runtimeLogger.WriteInfoLog(fileName, msg);
                    break;
            }

        }
    }
}
